import 'package:wheel_of_fortune/http/http_helper.dart';
import 'package:wheel_of_fortune/model/address_model.dart';
import 'package:wheel_of_fortune/model/candy_record_model.dart';
import 'package:wheel_of_fortune/model/check_win_model.dart';
import 'package:wheel_of_fortune/model/participate_model.dart';
import 'package:wheel_of_fortune/model/raffle%20_share_model.dart';
import 'package:wheel_of_fortune/model/raffle_model.dart';
import 'package:wheel_of_fortune/model/rotary_table_model.dart';
import 'package:wheel_of_fortune/model/user_%20lottery_model.dart';
import 'package:wheel_of_fortune/model/user_address_model.dart';

import 'http.dart';

class WheelService {
  /// ------------- rotary-table-info-front-controller
  /// get the current rotary table info  获取当前转盘信息
  Future<RotaryTableModel> getRotaryTableModel() async {
    var data = await Http().get(
      '/api/service/v1/rotary/table',
    );
    if (data == null) {
      return data;
    }
    return RotaryTableModel.fromJson(data);
  }

  /// user participate in the rotary table lottery  用户参与转盘抽奖
  Future getAward(String id) async {
    var data = await Http()
        .post('/api/service/v1/rotary/table/lottery', {'rotaryTableId': id});
    return data;
  }

  /// ---------- rotary-table-user-lottery-front-controller
  /// get the user rotary table lottery list 获取用户转盘抽奖单
  Future<List<UserLotteryModel>> getUserLotteryList() async {
    var data = await Http().get(
      '/api/service/v1/rotary/table/user/lottery',
    );
    List<UserLotteryModel> list = [];
    (data as List).forEach((item) {
      list.add(UserLotteryModel.fromJson(item));
    });
    return list;
  }

  /// gets the user's address list  获取用户的地址列表
  Future<List<UserAddressModel>> getUserLotteryAddress() async {
    var data = await Http().get(
      '/api/service/v1/rotary/table/user/lottery/address',
    );
    List<UserAddressModel> list = [];
    for (var i = 0; i < (data as List).length; i++) {
      final item = UserAddressModel.fromJson((data as List)[i]);
      if (i == 0) {
        item.selectAddress = true;
      }
      list.add(item);
    }
    return list;
  }

  /// users use the wallet amount to buy a lottery  用户使用钱包金额购买彩票
  Future buyALottery(String id) async {
    var data = await Http().post(
        '/api/service/v1/rotary/table/user/lottery/wallet/buy',
        {'rotaryTableId': id});
    return data;
  }

  /// user give up prize
  Future giveUpPrize(String id) async {
    var data = await Http().post(
        '/api/service/v1/rotary/table/user/lottery/give/up', {'lotteryId': id});
    return data;
  }

  /// user collect prize
  Future collectPrize(String id, String address, int cityMunicipalityId,
      String cityName, int provinceId, String provinceName) async {
    var data = await Http().post(
      '/api/service/v1/rotary/table/user/lottery/collect',
      {
        "lotteryId": id,
        "cityMunicipalityId": cityMunicipalityId ?? '',
        "cityName": cityName ?? '',
        "provinceId": provinceId ?? '',
        "provinceName": provinceName ?? '',
        "address": address ?? '',
      },
    );
    return data;
  }

  /// presented lottery after successful top up  成功充值后赠送彩票
  Future topUpPresented(double amount) async {
    var data = await Http().post(
        '/api/service/v1/rotary/table/user/lottery/top/up/presented',
        {'amount': amount});
    return data;
  }

  /// ---------- raffle-share-record-controller
  /// Get a raffle share info list    获取抽奖共享信息列表
  Future<List<RaffleShareModel>> getTheRaffleSharelist() async {
    var data = await Http().get('/api/service/v1/raffle/share');
    List<RaffleShareModel> list = [];
    (data as List).forEach((item) {
      list.add(RaffleShareModel.fromJson(item));
    });
    return list;
  }

  /// save Share information about winner of the raffle  保存有关抽奖获胜者的共享信息
  Future saveShareInformation(
      String id, String content, List<String> pictureUrls) async {
    var data = await Http().post('/api/service/v1/raffle/share',
        {'raffleInfoId': id, 'content': content, 'pictureUrls': pictureUrls});
    return data;
  }

  /// Save share like info  保存共享信息
  Future saveShareILikeInfo(
    String id,
  ) async {
    var data = await Http().post('/api/service/v1/raffle/share/like', {
      'id': id,
    });
    return data;
  }

  /// ---------- raffle-participate-user-record-controller
  /// To obtain user participation records of raffle 获取用户参与抽奖记录
  Future<List<RaffleRecordModel>> getRaffleRecords(String status) async {
    // status - START、ONGOING、OVER
    var data = await Http().get(
      '/api/service/v1/raffle/participate/user/record',
      params: {
        'status': status,
      },
    );
    List<RaffleRecordModel> list = [];
    (data as List).forEach((item) {
      list.add(RaffleRecordModel.fromJson(item));
    });
    return list;
  }

  /// ---------- raffle-info-controller
  ///  Check if any new raffle are win  检查是否有新的抽奖活动中奖
  Future<List<CheckWinModel>> checkWin() async {
    var data = await Http().get(
      '/api/service/v1/raffle/info/check/win',
    );
    List<CheckWinModel> list = [];
    (data as List).forEach((item) {
      list.add(CheckWinModel.fromJson(item));
    });
    return list;
  }

  /// Get the Raffle list based on the status  根据状态获取抽奖名单
  Future<List<RaffleModel>> getTheRafflelist(String status) async {
    // status - START、ONGOING、OVER
    var data = await Http()
        .get('/api/service/v1/raffle/info', params: {"status": status});
    List<RaffleModel> list = [];
    (data as List).forEach((item) {
      list.add(RaffleModel.fromJson(item));
    });
    return list;
  }

  /// User participate in the raffle  用户参与抽奖
  Future userParticipate(String id, int count, String pin) async {
    var data = await Http().post('/api/service/v1/raffle/info',
        {'rotaryTableId': id, 'count': count, 'pin': pin});
    List<ParticipateModel> list = [];
    (data as List).forEach((item) {
      list.add(ParticipateModel.fromJson(item));
    });
    return data;
  }

  /// Get information about winner of the raffle  获取抽奖优胜者的信息
  Future<WinnerInformationModel> getInformationOfWinner(String id) async {
    var data = await Http()
        .get('/api/service/v1/raffle/info/winner', params: {"id": id});
    return WinnerInformationModel.fromJson(data);
  }

  /// Get raffle details based on id   根据id获取抽奖详情
  Future<RaffleModel> getRaffleInfo(String id) async {
    var data = await Http().get('/api/service/v1/raffle/info/$id');
    return RaffleModel.fromJson(data);
  }

  /// ---------- candy-controller
  /// the candy exchanged rotary table lottery   糖果交换转盘彩票
  Future candyExchange(String id, int count) async {
    var data = await Http().post(
        '/api/service/v1/candy/exchange/rotary/table/lottery',
        {'rotaryTableId': id, 'count': count});
    return data;
  }

  /// get the user candy balance  获取用户糖果余额
  Future getCandyBalance() async {
    var data = await Http().get(
      '/api/service/v1/candy/balance',
    );
    return data;
  }

  //
  Future<List<CandyRecordModel>> getCandyRecord() async {
    var data = await Http().get(
      '/api/service/v1/candy/record',
    );
    List<CandyRecordModel> list = [];
    (data as List).forEach((item) {
      list.add(CandyRecordModel.fromJson(item));
    });
    return list;
  }

  /// region-list
  Future<List<RegionModel>> getRegionlist() async {
    var data =
        await Http(baseUrl: addressBaseUrl, authorization: addressAuthorization)
            .get(
      '/api/region-list',
    );
    List<RegionModel> list = [];
    (data as List).forEach((item) {
      list.add(RegionModel.fromJson(item));
    });
    return list;
  }

  /// province-list
  Future<List<ProvinceModel>> getProvincelist(regionCode) async {
    var data = await Http(
            baseUrl: addressBaseUrl, authorization: addressAuthorization)
        .get('/api/province-list',
            params: regionCode == null ? null : {'region_code': regionCode});
    List<ProvinceModel> list = [];
    (data as List).forEach((item) {
      list.add(ProvinceModel.fromJson(item));
    });
    return list;
  }

  /// city-list
  Future<List<CityModel>> getCitylist(provinceCode) async {
    var data =
        await Http(baseUrl: addressBaseUrl, authorization: addressAuthorization)
            .get('/api/city-municipality-list',
                params: provinceCode == null
                    ? null
                    : {'province_code': provinceCode});
    List<CityModel> list = [];
    (data as List).forEach((item) {
      list.add(CityModel.fromJson(item));
    });
    return list;
  }
}
